#$ARGV[0] is the 0/1 model log file.
#$ARGV[1] is start positiion.
#$ARGV[2] is end positiion.

$file = $ARGV[0];

$startP = 1;
$endP = 200;

if($ARGV[1]!=0){
	$startP = $ARGV[1];
}
if($ARGV[2]!=0){
	$endP = $ARGV[2];
}
$totalNum = $endP-$startP+1;
print "totalNum: $totalNum\n";
# if($ARGV[1]!=0){
	# $totalNum = $ARGV[1];
# }
# else {
	# $totalNum = 10000000;
# }

@pathname = split(/\\/,$file);
$len = @pathname;
$path;
for($i=$len-2;$i>=0;$i--){
	$path = "$pathname[$i]\\"."$path";
	print "$path\n";
}

$filename;
@tempfile = split(/\./,$pathname[$len-1]);
$filename = $tempfile[0];
print "file: $filename\n";

$channelNum = 16;
@samples;

@BBCMatric;
@IBCMatric;
@BICMatric;
@IICMatric;

@totalIdle;
@totalBusy;
for($i=0;$i<$channelNum+1;$i++){
	$totalBusy[$i] = 0;
	$totalIdle[$i] = 0;
}

for($i=0;$i<$channelNum+1;$i++){
	for($j=0;$j<$channelNum+1;$j++){
		$BBCMatric[$i][$j]=0;
		$IBCMatric[$i][$j]=0;
		$BICMatric[$i][$j]=0;
		$IICMatric[$i][$j]=0;
	}
}

#get the samples from log file
$alreadyNum = 0;
$beforeStart=1;
open fin, "<$file" or die "can not open file $file!\n";
GETSAMPLE:{
	while (<fin>) {
		chomp;
		@info = split;

		$length = @info;
		if(($length-1)!=$channelNum){
			
			last GETSAMPLE;
		}
		if($beforeStart<$startP){
			$beforeStart++;
			next;
		}
		
		for($i=1;$i<$channelNum+1;$i++){
			$samples[$alreadyNum][$i]=$info[$i];
			if($samples[$alreadyNum][$i]==1){
				$totalBusy[$i]++;
			}
			else {
				$totalIdle[$i]++;
			}
		}
		$alreadyNum++;
		if($alreadyNum==$totalNum){
			last GETSAMPLE;
		}
	}
}
close fin;
$totalNum=$alreadyNum;
print "totalNum: $totalNum\n";
open OR, ">$path\\occupRatioCondMatric_$filename.dat" or die "can not open file .\\occupRatioCondMatric.dat\n";
for($i=1;$i<$channelNum+1;$i++){
	$occupRatio = $totalBusy[$i]/$totalNum;
	$idleRatio = $totalIdle[$i]/$totalNum;
	print "totalBusy $i: $totalBusy[$i] $totalIdle[$i]\n";
	print OR "$i $totalBusy[$i] $totalIdle[$i] $occupRatio $idleRatio\n";
}
close OR;

#calculate the conditional probabilities
for($i=1;$i<$channelNum+1;$i++){
	for($k=0;$k<$totalNum;$k++){
		for($j=1;$j<$channelNum+1;$j++){
			#conditional channel is Busy.
			if($samples[$k][$i]==1){
				if($samples[$k][$j]==1){
					$BBCMatric[$i][$j]++;
				}
				else{
					$IBCMatric[$i][$j]++;
				}
			}
			#conditional channel is Idle.
			else{
				if($samples[$k][$j]==1){
					$BICMatric[$i][$j]++;
				}
				else{
					$IICMatric[$i][$j]++;
				}
			}
		}
	}
}

#i is the conditional channel. 
for($i=1;$i<$channelNum+1;$i++){
	for($j=1;$j<$channelNum+1;$j++){
		if($totalBusy[$i]!=0){
			$BBCMatric[$i][$j] /= $totalBusy[$i];
			$IBCMatric[$i][$j] /= $totalBusy[$i];
		}
		if($totalIdle[$i]!=0){
			$BICMatric[$i][$j] /= $totalIdle[$i];
			$IICMatric[$i][$j] /= $totalIdle[$i];
		}
	}
}


open BB, ">$path\\BBMatric_$filename.dat" or die "can not open file .\\BBMatric.dat\n";
open IB, ">$path\\IBMatric_$filename.dat" or die "can not open file .\\IBMatric.dat\n";
open BI, ">$path\\BIMatric_$filename.dat" or die "can not open file .\\BIMatric.dat\n";
open II, ">$path\\IIMatric_$filename.dat" or die "can not open file .\\IIMatric.dat\n";
for($i=1;$i<$channelNum+1;$i++){
	for($j=1;$j<$channelNum+1;$j++){
		print BB "$BBCMatric[$i][$j] ";
		print IB "$IBCMatric[$i][$j] ";
		print BI "$BICMatric[$i][$j] ";
		print II "$IICMatric[$i][$j] ";
	}
	print BB "\n";
	print IB "\n";
	print BI "\n";
	print II "\n";
}
close BB;
close IB;
close BI;
close II;



